<?php

namespace app\api\model;

use think\Model;

class Sensitive extends Model
{
    /**
     * [createData 新建]
     */
    public function createData($param)
    {
        // 验证
        try {
            $param['create_time'] = time();
            $param['update_time'] = time();
            $this->data($param)->allowField(true)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * [updateDataById 编辑]
     * @linchuangbin
     * @DateTime  2017-02-10T21:24:49+0800
     * @param     [type]                   $param [description]
     * @param     [type]                   $id    [description]
     * @return    [type]                          [description]
     */
    public function updateDataById($param, $id)
    {
        $checkData = $this->get($id);
        if (!$checkData) {
            $this->error = '暂无此数据';
            return false;
        }

        try {
            $this->allowField(true)->save($param, [$this->getPk() => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '编辑失败';
            return false;
        }
    }

    public function category() {
        return $this->hasOne(SensitiveCategory::class, SensitiveCategory::ID, 'cid');
    }

    public function words() {
        return $this->hasMany(SensitiveWord::class, SensitiveWord::SID, 'id');
    }
}
